// globals
float4x4	World;
float4x4	gWVP;
texture		imageTexture;
float4x4	gBindPose[25]; // inverted already
float4x4	gCurrentPose[25];

sampler tex
{
	Texture = <imageTexture>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU = WRAP;
	AddressV = WRAP;
};

struct InputVS
{
	float3 position:POSITION0;
	float3 normal:COLOR0;
	float2 uv:TEXCOORD0;
	float3 weights:BLENDWEIGHT0;
	float3 indices:BLENDINDICES0;
};

// Define a vertex shader output structure;
struct OutputVS
{
    float4 posH:POSITION0;
    float4 normal:COLOR0;
    float2 texCoord0: TEXCOORD0;
    float2 depth: TEXCOORD1;
	float index : POSITION1;
	float weight : POSITION2;
};

struct OutputPS
{
	float4 color: COLOR0;
	float4 normal: COLOR1;
	half4 depth: COLOR2;
};

// Vertex shader -------------------------------------------------------------------------------------
OutputVS TransformVS( InputVS input )
{
	// output structure
	OutputVS outVS;
	
	// scale the triangle
	outVS.posH = float4(0,0,0,1.0f);//mul( float4( input.position, 1.0f ), gWVP );
	outVS.normal = float4(0,0,0,1.0f);//mul( float4( input.normal, 1.0f ), World );
	outVS.texCoord0 = input.uv;
	outVS.depth.x = outVS.posH.z;
	outVS.depth.y = outVS.posH.w;
	
	float4 result;
	float4 normResult;
	float4 normal = float4( input.normal, 1.0f );
	float4 position = float4( input.position, 1.0f );
	
	result = mul(position, gBindPose[input.indices.x]);
	normResult = mul(normal, gBindPose[input.indices.x]);
	result = input.weights.x * mul(result, gCurrentPose[input.indices.x]);
	outVS.posH += result;
	outVS.normal += input.weights.x * mul(result, gCurrentPose[input.indices.x]);
	
	outVS.index = input.indices.x;
	//outVS.bind = gCurrentPose[0];
	outVS.weight = input.weights.x;
	//float4x4 curr = gCurrentPose[index];
	
	result = mul(position, gBindPose[input.indices.y]);
	normResult = mul(normal, gBindPose[input.indices.y]);
	outVS.posH += input.weights.y * mul(result, gCurrentPose[input.indices.y]);
	outVS.normal += input.weights.y * mul(result, gCurrentPose[input.indices.y]);
	
	result = mul(position, gBindPose[input.indices.z]);
	normResult = mul(normal, gBindPose[input.indices.z]);
	outVS.posH += input.weights.z * mul(result, gCurrentPose[input.indices.z]);
	outVS.normal += input.weights.z * mul(result, gCurrentPose[input.indices.z]);
	
	outVS.posH = mul(outVS.posH, gWVP);
	outVS.normal = mul(outVS.normal, World);
	
    return outVS;
}


// Pixel Shader ---------------------------------------------------------------------------
OutputPS TransformPS( OutputVS outVS )
{
	// change the color of the triangle
	OutputPS outPS;
	
	outPS.color = tex2D( tex, outVS.texCoord0 );
	outPS.depth = outVS.depth.x / outVS.depth.y;
	outPS.normal = outVS.normal;
	
	return outPS;
}

// ----------------------------------------------------------------------------------------
technique Basic
{
    pass P0
    {
        vertexShader = compile vs_3_0 TransformVS();
        pixelShader  = compile ps_3_0 TransformPS();
      
		ShadeMode = Gouraud;
		
        //FillMode = WIREFRAME;
        
        CullMode = CW;
    }
}
